package com.kunteng.ynhxksh.utils;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Administrator on 2016/7/28.
 */
public class DateUtil {
    public static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static long dateStartTime(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(s);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /*
    * 获取当前的年份
    * */
    public static int getNowYear(){
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR);
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String s) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime() / 1000;
        return ts;
    }

    /*
     * 将时间转换为时间戳(2016-10-19-11)
     */
    public static long dateToStampHour(String s) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime() / 1000;
        return ts;
    }

    /*
    *获取昨天的时间
    * 格式yyyy-MM-dd
    * */
    public static String getDayOfYesterday() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        String yesterday = dateFormat.format(cal.getTime());
        return yesterday;
    }

    /*
    *获取昨天的时间
    * 格式yyyy/MM/dd
    * */
    public static String getDayOfYesterdays() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        String yesterday = dateFormat.format(cal.getTime());
        return yesterday;
    }

    /*
    *获取昨天的时间
    * 格式yyyyMMdd
    * */
    public static String getDayOfYesterdayL() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        String yesterday = dateFormat.format(cal.getTime());
        return yesterday;
    }

    /*
    *获取昨天的时间
    * 格式yyyy-MM-dd
    * */
    public static String getDayOfYesterdayG() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        String yesterday = dateFormat.format(cal.getTime());
        return yesterday;
    }

    /**
     * 时间戳转换成String
     *
     * @param s
     * @return
     * @throws ParseException
     */
    public static String stampTodate(Long s) throws ParseException {
        //时间戳转化为Sting或Date
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = format.format(s * 1000);
        return d;
    }

    /*
     * 将时间转换为时间戳(2016-10-19)
     */
    public static long dateToStampDay(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime() / 1000;
        return ts;
    }

    /*
 *获取当天日期 前i天的日期
 * 格式yyyy-MM-dd
 * */
    public static String getDayOfBe(int i) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -i);
        String yesterday = dateFormat.format(cal.getTime());
        return yesterday;
    }


    /*
    *获取当天的时间
    * 格式yyyy-MM-dd-hh
    * */
    public static String getDateNowHour() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH");
        String nowTime = dateFormat.format(new Date());
        return nowTime;
    }

    /*
    *获取当天的时间
    * 格式yyyyMMdd HH:mm
    * */
    public static String getDateNowByStyle1() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm");
        String nowTime = dateFormat.format(new Date());
        return nowTime;
    }

    /*
    *获取当天的时间
    * 格式yyyy-MM-dd-hh
    * */
    public static String getDateNowDay() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String nowTime = dateFormat.format(new Date());
        return nowTime;
    }

    /*
  *获取当前的时间
  * 格式yyyy-MM-dd HH:mm:ss
  * */
    public static String getDateNowss() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = dateFormat.format(new Date());
        return nowTime;
    }

    /*
  *获取明天的时间
  * 格式yyyy-MM-dd
  * */
    public static String getDayOfTomorrow() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        String tomorrow = dateFormat.format(cal.getTime());
        return tomorrow;
    }


    /**
     *
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }

    //获取距离dateTime多少天的时间，前+ ,后-
    public static String getDay(String dateTime, int i) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = df.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = (long)date.getTime() - (long)i * 3600 * 24 * 1000;
        return df.format(new Date(time));
    }

    /**
     * 两个时间之间相差距离多少天
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceDays(String str1, String str2) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days + 1;
    }

    public static void main(String[] args) {
//        System.out.println(getDayOfTomorrow());
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Calendar cal = new GregorianCalendar();
//        cal.setTime(new Date());
//        cal.add(Calendar.DAY_OF_MONTH, -30);
//        String date= dateFormat.format(cal.getTime());
//        System.out.println(date);
//            String channelPath = "1#BSOJ4#MA12S#DK3J0#$CFDRR2";
//            String[] cids = channelPath.split("#");
//            List<String> cid =  Arrays.asList(cids);
//            System.out.println(cid);

//        Long t = System.currentTimeMillis();
//        System.out.println(t);
//        System.out.println(t/1000);
///**
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("thirdpartyId", "2256");
        requestEntity.add("memCard", "833061f73ac94e17ae2be608fb970c25"); //yuan
        requestEntity.add("add", "1");
        requestEntity.add("sign", "4c73da9d24cc7b4890638b053a8c711c");
        RestTemplate restTemplate=new RestTemplate();
        String s1 = restTemplate.postForObject("http://vote.cn-healthcare.com/thirdparty/801d8620-677a-4774-851b-8a0686949866/vote", requestEntity, String.class);
        System.out.println("status1="+s1);
        long L1=System.currentTimeMillis();
        long L2=0L;
        try {
            Thread.sleep(10000);
            L2=System.currentTimeMillis();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println(L2-L1);
        }
        MultiValueMap<String, String> requestEntity2 = new LinkedMultiValueMap<>();
        requestEntity2.add("thirdpartyId", "2256");
        requestEntity2.add("memCard", "2dbf87dc731e4f9d890f1a064a0a86bf");  //chen
        requestEntity2.add("add", "1");
        requestEntity2.add("sign", "5e3652c9dc50ba3c5ee4df9b7e2d0c85");
        RestTemplate restTemplate2=new RestTemplate();
        String s2 = restTemplate2.postForObject("http://vote.cn-healthcare.com/thirdparty/801d8620-677a-4774-851b-8a0686949866/vote", requestEntity, String.class);
        System.out.println("status2="+s2);
// **/
//        System.out.println("833061f73ac94e17ae2be608fb970c25".length());
    }

    public static String percentage(float date) {
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(2); //最大小数位数
//            percentFormat.setMaximumIntegerDigits(3);//最大整数位数
        percentFormat.setMinimumFractionDigits(2); //最小小数位数
//            percentFormat.setMinimumIntegerDigits(1);//最小整数位数
        return percentFormat.format(date);
    }
}
